﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Procure.Data.Context;
using umbraco.interfaces;

namespace Procure.Web.productType
{
    public class productTypeTasks : ITaskReturnUrl
    {
        public int UserId { get; set; }
        public int TypeID { get; set; }
        public int ParentID { get; set; }
        public string Alias { get; set; }
        public string ReturnUrl { get; private set; }

        public bool Save()
        {
            try
            {
                Procure.Data.Context.ProcureDataContext context = DataFactory.GetDataContext();
                ProductType pt = new ProductType();
                pt.Name = Alias;
                context.ProductTypes.InsertOnSubmit(pt);
                context.SubmitChanges();

                ReturnUrl = string.Format("Procure/editProductType.aspx?id={0}", pt.Id);
            }
            catch (Exception ex)
            {
                ReturnUrl = string.Format("Procure/{0}.aspx", ex.Message);
            }

            return true;
        }

        public bool Delete()
        {
            Procure.Data.Context.ProcureDataContext context = DataFactory.GetDataContext();

            ProductType type = context.ProductTypes.FirstOrDefault(pt => pt.Id == ParentID);
            if (type != null)
            {
                context.ProductTypes.DeleteOnSubmit(type);
                context.SubmitChanges();
                return true;
            }
            else
                return false;
        }

        public productTypeTasks()
        {

        }

        
    }
}